package com.duoqin.emoji;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.duoqin.widget.R;

import pl.droidsonroids.gif.GifImageView;

public class EmojiInputView extends RelativeLayout implements AdapterView.OnItemClickListener {
    private GridView mGridView;
    private EmojiGridAdapter mEmojiGridAdapter;

    public interface OnItemClickListener {
        void onItemClick(String emojiString, int emojiIcon);
    }

    private OnItemClickListener mOnItemClickListener;

    public EmojiInputView(Context context) {
        super(context);
    }

    public EmojiInputView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public EmojiInputView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    protected void onFinishInflate() {
        super.onFinishInflate();
        mGridView = findViewById(R.id.emojiGridView);
        mEmojiGridAdapter = new EmojiGridAdapter();
        mGridView.setAdapter(mEmojiGridAdapter);
        mGridView.setSelection(0);
        mGridView.setOnItemClickListener(this);
    }

    public void setSelection(int selection) {
        if (mGridView != null) {
            mGridView.setSelection(selection);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(Emoji.EmojiPatterns[position],
                    Emoji.EmojiIcons[position]);
        }
    }

    private class EmojiGridAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return Emoji.EmojiIcons.length;
        }

        @Override
        public Object getItem(int position) {
            return Emoji.EmojiIcons[position];
        }

        @Override
        public long getItemId(int position) {
            return Emoji.EmojiIcons[position];
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            int width = mGridView.getColumnWidth();
            if (convertView == null) {
                convertView = new GifImageView(getContext());
                ((GifImageView) convertView).setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            }
            if (position >= 0 && position < Emoji.EmojiIcons.length) {
                ((GifImageView) convertView).setImageResource(Emoji.EmojiIcons[position]);
            }
            convertView.setLayoutParams(new GridView.LayoutParams(width, width));
            return convertView;
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
